<template>
  <div class="btn-box" v-if="type == 2">
    <!-- 团员 type==2 -->
    <div
      class="dbtn db0"
      v-if="getStatus(data, '申请退款', type)"
      @click.stop="handleRefound"
    >
      申请退款
    </div>
    <div
      class="dbtn db0"
      v-if="getStatus(data, '售后详情', type)"
      @click.stop="handleSalesDetails"
    >
      售后详情
    </div>
    <div
      class="dbtn"
      v-if="getStatus(data, '立即支付', type)"
      @click.stop="handlePay"
    >
      立即支付
    </div>
    <div
      class="dbtn"
      v-if="getStatus(data, '确认收货', type)"
      @click.stop="handleConfirm"
    >
      确认收货
    </div>
  </div>
  <div class="btn-box" v-if="type == 1">
    <!-- 团长 type==1 -->

    <div
      class="dbtn db0"
      v-if="getStatus(data, '退款', type)"
      @click.stop="handleRefound"
    >
      退款
    </div>
    <div
      class="dbtn db0"
      v-if="getStatus(data, '售后详情', type)"
      @click.stop="handleSalesDetails"
    >
      售后详情
    </div>

    <div
      class="dbtn"
      v-if="getStatus(data, '确认核销', type)"
      @click.stop="handleConfirm"
    >
      确认核销
    </div>
  </div>
</template>

<script lang="ts" setup>
import { getPayInfoApi } from "@/api/order/index";
import { isRes } from "@/utils/pub-use";
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance() as any;

// type: 1 // 团长   2//tuanyuan
const props = defineProps(["data", "type"]);
const emits = defineEmits(["confirm", "refound", "pay"]);
/**
 *
 * @param n  数据 {status,status_type}
 * @param btnName 按钮状态
 * @param type  1:团长 2团员
 */

const setStatus = (n: {
  status: string | number;
  status_type: number | string;
}) => {
  if (n.status == 1) {
    if (n.status_type == 0) {
      return "待发货";
    }
    if (n.status_type == 1) {
      return "已发货";
    }
    if (n.status_type == 2) {
      return "已提货";
    }
    if (n.status_type == 3) {
      return "已收货";
    }
    if (n.status_type == 4) {
      return "待提货";
    }

    return "已付款";
  }
  if (n.status == 0) {
    return "待支付";
  }
  if (n.status == 1) {
    return "已取消";
  }
  if (n.status == 90) {
    return "待退款";
  }

  if (n.status == 98) {
    return "已取消";
  }
  if (n.status == 99) {
    return "已退款";
  }
  if (n.status == 100) {
    return "拒绝退款";
  }
};
const getStatus = (
  n: {
    status: string | number;
    status_type: number | string;
    is_allowed_refund: any;
  },
  btnName?: string,
  type?: number | string
) => {
  let btntitle: any = setStatus(n);

  if (type == 1) {
    //团长
    if (btnName == "退款") {
      if (!n.is_allowed_refund) return false;
      if (
        btntitle == "待退款" ||
        btntitle == "待发货" ||
        btntitle == "已发货" ||
        btntitle == "待提货"
      ) {
        return true;
      } else {
        return false;
      }
    }
    if (btnName == "售后详情") {
      if (
        btntitle == "已取消" ||
        btntitle == "拒绝退款" ||
        btntitle == "已退款"
      ) {
        return true;
      } else {
        return false;
      }
    }
    if (btnName == "确认核销") {
      if (btntitle == "待提货" || btntitle == "已发货") {
        return true;
      } else {
        return false;
      }
    }
    return false;
  } else if (type == 2) {
    // 团员
    if (btnName == "申请退款") {
      if (!n.is_allowed_refund) return false;
      if (
        btntitle == "已支付" ||
        btntitle == "待发货" ||
        btntitle == "待提货" ||
        btntitle == "已发货"
      ) {
        return true;
      } else {
        return false;
      }
    }
    if (btnName == "立即支付") {
      if (btntitle == "待付款" || btntitle == "待支付") {
        return true;
      } else {
        return false;
      }
    }
    if (btnName == "确认收货") {
      if (btntitle == "待提货" || btntitle == "已发货") {
        return true;
      } else {
        return false;
      }
    }

    if (btnName == "售后详情") {
      if (
        btntitle == "待退款" ||
        btntitle == "已取消" ||
        btntitle == "已退款" ||
        btntitle == "拒绝退款"
      ) {
        return true;
      } else {
        return false;
      }
    }

    return false;
  }
};

const router = useRouter();

const handleRefound = () => {
  emits("refound", props.data);
};
const handleConfirm = () => {
  emits("confirm", props.data);
};

const handleSalesDetails = () => {
  console.log(props.data);
  router.push({
    path: "/salesDetails",
    query: { order_id: props.data.order_id },
  });
};

//-------------------------------获取订单信息-------------------------------
const handlePay = async () => {
  let res = await getPayInfoApi(props.data.order_no);
  if (isRes(res)) {
    proxy.$cxpay.startPayment(
      res.result.payorder_no,
      res.result.total_fee,
      res.result.order_body,
      "城信余额支付",
      "chengxinPaycallback"
    );
  }
};

onMounted(() => {
  window.addEventListener("setItem", (e) => {
    let newV = sessionStorage.getItem("pay_status");
    if (newV == "CANCEL" || newV == "FAIL") {
    } else if (newV == "SUCCESS") {
      router.push("/home");
    }
  });
});
</script>
<style scoped lang="scss">
.btn-box {
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
  gap: 20px;
  .btn {
    padding: 30px 0px 0 30px;
    width: 146px;
    font-size: 26px;
    font-weight: 400;
    color: #40ae36;
    border: 2px solid #40ae36;
    background: #f9fffa;
    text-align: center;
    border-radius: 50px;
    padding: 10px 0;
  }
  .dbtn {
    width: 146px;
    font-size: 26px;
    font-weight: 400;
    color: #40ae36;
    border: 2px solid #40ae36;
    background: #f9fffa;
    text-align: center;
    border-radius: 50px;
    padding: 10px 0;
  }
  .db0 {
    color: #000;
    border: 2px solid #999;
  }
}
</style>
